package com.pearadmin.pro.modules.ser.service.impl;

import cn.hutool.core.util.IdUtil;
import com.pearadmin.pro.modules.ser.pojo.FileDTO;
import com.pearadmin.pro.modules.ser.pojo.SFTPConnectInfo;
import com.pearadmin.pro.modules.ser.pojo.WebSSHData;
import com.pearadmin.pro.modules.ser.service.SftpService;
import com.pearadmin.pro.modules.ser.util.SFTPUtil;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Author: didiplus
 * Email: 972479352@qq.com
 * CreateTime: 2022/3/15
 * Desc:
 */
@Service
public class SftpServiceImpl implements SftpService {
    /**
     * 存放sftp连接信息的map
     */
    private static Map<String, Object> sftpMap = new ConcurrentHashMap<>();
    @Override
    public String connect(WebSSHData webSSHData) throws Exception {
        SFTPConnectInfo sftpConnectInfo = new SFTPConnectInfo();
        SFTPUtil.getConnect(sftpConnectInfo,webSSHData);
        String keys = IdUtil.simpleUUID();
        sftpMap.put(keys,sftpConnectInfo);
        return keys;
    }

    @Override
    public List<FileDTO> listFiles(String keys, String path) throws Exception {
        SFTPConnectInfo sftpConnectInfo =(SFTPConnectInfo)sftpMap.get(keys);
        return SFTPUtil.listFiles(path,sftpConnectInfo);
    }
}
